import { Vec2, Vec3, Vec4 } from "wgpu-matrix";
import { Texture2DAsset } from "../../asset/texture-asset";
import { WebGpuShader } from "./webgpu-shader";

abstract class MaterialUniformSlot {
    name: string

    protected constructor(name: string) {
        this.name = name
    }
}

class TextureUniformSlot extends MaterialUniformSlot{    
    texture: WeakRef<Texture2DAsset>

    constructor(name: string, texture: Texture2DAsset) {
        super(name)

        this.texture = new WeakRef(texture)
    }
}

class BufferUniformSlot extends MaterialUniformSlot {
    buffer: Float32Array

    constructor(name: string, buffer: Float32Array) {
        super(name)
        this.buffer = buffer
    }
} 

export class WebGpuMaterial {
    shader: WebGpuShader
    uniforms: Map<string, MaterialUniformSlot>

    private _setUniform(name: string, u: MaterialUniformSlot) {
        this.uniforms.set(name, u)
    }

    setVec2(name: string, v: Vec2) {
        this._setUniform(name, new BufferUniformSlot(name, v))
    }

    setVec3(name: string, v: Vec3) {
        this._setUniform(name, new BufferUniformSlot(name, v))
    }
    
    setVec4(name: string, v: Vec4) {
        this._setUniform(name, new BufferUniformSlot(name, v))
    }

    setTexture(name: string, tex: Texture2DAsset) {
        this._setUniform(name, new TextureUniformSlot(name, tex))
    }
}
